﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

public partial class Chain_List : PageBase
{
    private const string NEW_PAGE = "Chain_Maintain.aspx";
    private ChainManager manager = new ChainManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindGrid();
        }
    }

    protected void SelectALL(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk == null)
            return;
        foreach (GridViewRow row in this.gvChannelType.Rows)
        {
            CheckBox checkbox = row.Cells[gvChannelType.Columns.Count - 1].FindControl("cbxSelected") as CheckBox;
            if (checkbox != null)
                checkbox.Checked = chk.Checked;
        }
    }


    private void BindGrid()
    {
        int status;
        if (string.IsNullOrEmpty(ddlStatus.SelectedValue))
            status = 2;
        else
            status = int.Parse(ddlStatus.SelectedValue);
        gvChannelType.DataSource = new ChainManager().GetChainsList(txtCode.Text.Trim(), txtChain.Text.Trim(), MyStateManager.OrgID.ToString(), status);
        gvChannelType.DataBind();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect(NEW_PAGE);
    }
    protected void gvChannelType_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        object oID = e.CommandArgument;
        if (e.CommandName == "EditBanner" && oID != null)
        {
            Response.Redirect(NEW_PAGE + "?id=" + oID.ToString());
        }
    }
    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnAssign_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvChannelType.Rows)
        {
            CheckBox cbx = (CheckBox)row.FindControl("cbxSelected");
            int chainID = int.Parse(gvChannelType.DataKeys[row.RowIndex].Values["ID"].ToString());
            bool include = gvChannelType.DataKeys[row.RowIndex].Values["INCLUDE"].ToString() == "1";

            //新增
            if (cbx.Checked && include==false)
            {
                if (manager.LoadChainOrg(chainID, MyStateManager.OrgID) != null)
                {
                    ShowMessage("请不要重复添加!");
                    BindGrid();
                    return;
                }
                CHAINS_ORG org = new CHAINS_ORG();
                org.CHAINID = chainID;
                org.ORGID = MyStateManager.OrgID;
                org.LAST_UPDATE_TIME = DateTime.Now;
                //org.LAST_UPDATER = MyStateManager.Name;
                org.Save();
                continue;
            }

            //删除
            if (!cbx.Checked && include == true)
            {
                manager.DeleteChainsORG(chainID, MyStateManager.OrgID);
                continue;
            }

        }

        BindGrid();

    }
}
